﻿
using Kylin.Wiki.IService;
using Kylin.Wiki.Model.Api;
using Kylin.Wiki.Model.Dto;
using Kylin.Wiki.Model.Entity;
using Kylin.Wiki.Web.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Kylin.Wiki.Web.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : BaseController
    {
        private readonly IRoleService _roleService;

        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }



        /// <summary>
        /// 获取全部菜单角色
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetList")]
        public ApiResult<List<Role>> GetList()
        {
            ApiResult<List<Role>> result = new ApiResult<List<Role>>();
            result.Data = _roleService.Find(x => 1 == 1).OrderBy(x => x.RoleID).ToList();
            return result;
        }

        [HttpGet("GetListByPage")]
        public ApiPageResult<List<Role>> GetListByPage(RoleDTO roleDTO)
        {
            return _roleService.GetListByPage(roleDTO);
        }

        [HttpPost]
        public ApiResult<bool> Create([FromBody] RoleUpdateDTO role)
        {
            role.CreateBy = UserContext.Current.UserName;
            role.CreateOn = DateTime.Now;
            role.UpdateBy = UserContext.Current.UserName;
            role.UpdateOn = DateTime.Now;
            role.IsDelete = "0";
            role.Status = false;
            return _roleService.Create(role);
        }
        [HttpPut]
        public ApiResult<bool> Update([FromBody] RoleUpdateDTO role)
        {
            role.UpdateBy = UserContext.Current.UserName;
            role.UpdateOn = DateTime.Now;
            return _roleService.Update(role);
        }
        [HttpDelete("{id}")]
        public ApiResult<bool> Delete(int id)
        {
            var role = _roleService.FindFirst(x => x.RoleID == id);
            if (role == null)
                return new ApiResult<bool>(false, Core.Enum.HttpStateCode.ParameterError, "该角色不存在");
            return _roleService.Delete(role); 
        }
    }
}
